﻿using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Hosting;
using System.Security.Cryptography;
using System.Text;
using System.IO;
using System.Security.Claims;
using Microsoft.AspNetCore.Http.Authentication;
using Microsoft.AspNetCore.Authorization;
using Stock.Core;
using Stock.Core.Common;
using Stock.Model;
using Stock.Model.ViewModels;
using Stock.Core.Service;
using System.Collections.Generic;
using Stock.Model.Entities;
using Newtonsoft.Json;

// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace Web.Controllers
{
    public class OrderController : BaseApiController
    {
        private readonly IOrderService _s;

        public OrderController(IOrderService s)
        {
            _s = s;
        }

        /// <summary>
        /// 新增订单
        /// </summary>
        /// <returns>The insert.</returns>
        /// <param name="model">Model.</param>
        [HttpPost()]
        public async Task<dynamic> Insert([FromBody]order model)
        {

            //if (DateTime.Now.DayOfWeek == DayOfWeek.Sunday )
            //{
            //    return new BaseResult() { IsSuccess = false, Message = "周末无法下单" };
            //}
            //else if (DateTime.Now.DayOfWeek == DayOfWeek.Saturday && DateTime.Now.Hour > 2)
            //{
            //    return new BaseResult() { IsSuccess = false, Message = "周末无法下单" };
            //}

            //if (DateTime.Now.Hour > 2 && DateTime.Now.Hour < 12)
            //{
            //    return new BaseResult() { IsSuccess = false, Message = "2点后无法下单" };
            //}

            model.status = 0;

            DateTime now = DateTime.Now;
            now = new DateTime(now.Year, now.Month, now.Day, now.Hour, now.Minute, now.Second);

            model.moneny =(long) model.count;
            model.count = model.count* 1.0 / 100;
            model.commission = 5 * model.count;
            model.time = TimeHelper.ConvertDateTimeInt(now.AddHours(-8));
            model.create_date = now;

            int addtime = (5 - now.Minute % 5) + 5;
            DateTime counttime = new DateTime(model.create_date.Year, model.create_date.Month, model.create_date.Day, model.create_date.Hour, model.create_date.Minute, 0);
            counttime = counttime.AddMinutes(addtime);
            model.count_time = new DateTime(counttime.Year, counttime.Month, counttime.Day, counttime.Hour, counttime.Minute, 0);

            NetManager.sendManagerMsg(JsonConvert.SerializeObject(new { type = 1000 }));

            return await _s.insert(model);
        }

        [HttpPost()]
        public async Task<dynamic> getList(DateTime starttime)
        {
            return await _s.getList(starttime);
        }

        /// <summary>
        /// 获取今天的订单
        /// </summary>
        /// <returns>The today my order.</returns>
        /// <param name="userid">Userid.</param>
        [HttpGet()]
        public async Task<dynamic> getTodayMyOrder(Guid userid)
        {

            DateTime time = DateTime.Now;
            DateTime today = new DateTime(time.Year, time.Month, time.Day);

            long today_start = TimeHelper.ConvertDateTimeInt(today.AddHours(-8));
            long today_end = TimeHelper.ConvertDateTimeInt(today.AddDays(1).AddHours(-8).AddMinutes(-1));


            var data = await _s.getMyOrders(userid, today_start, today_end);

            return new BaseResult() { Data = data, DataCount = data.Count, IsSuccess = true };
        }

        [HttpGet()]
        public async Task<dynamic> getMyUnCountOrders(Guid usrid)
        {
            DateTime time = DateTime.Now;
            DateTime today = new DateTime(time.Year, time.Month, time.Day);

            long today_start = TimeHelper.ConvertDateTimeInt(today.AddHours(-8));
            long today_end = TimeHelper.ConvertDateTimeInt(today.AddDays(1).AddHours(-8).AddMinutes(-1));


            var data = await _s.getMyUnCountOrders(usrid, today_start, today_end);

            return new BaseResult() { Data = data, DataCount = data.Count, IsSuccess = true };
        }

        [HttpGet()]
        public async Task<dynamic> getUnCountOrders()
        {
            DateTime time = DateTime.Now;
            DateTime today = new DateTime(time.Year, time.Month, time.Day);

            long today_start = TimeHelper.ConvertDateTimeInt(today.AddHours(-8));
            long today_end = TimeHelper.ConvertDateTimeInt(today.AddDays(1).AddHours(-8).AddMinutes(-1));


            var data = await _s.getUnCountOrders(today_start, today_end);

            return new BaseResult() { Data = data, DataCount = data.Count, IsSuccess = true };
        }

        [HttpGet()]
        public async Task<dynamic> getMyWeekOrders(Guid usrid)
        {

            DateTime endWeek = DateTime.Now;

            DateTime startWeek = endWeek.AddDays(-7); 

            var data = await _s.getWeekOrders(usrid, startWeek, endWeek);

            return new BaseResult() { Data = data, DataCount = data.Count, IsSuccess = true };
        }

        [HttpGet()]
        public async Task<dynamic> getMyMothOrders(Guid? usrid)
        {

            DateTime endWeek = DateTime.Now;

            DateTime startWeek = endWeek.AddDays(-30);

            var data = await _s.getWeekOrders(usrid, startWeek, endWeek);

            return new BaseResult() { Data = data, DataCount = data.Count, IsSuccess = true };
        }

        [HttpGet()]
        public async Task<dynamic> getOrders(Guid? usrid, DateTime? startDate, DateTime? endDate)
        {
            if(endDate == null)
            endDate = DateTime.Now;

            if (startDate == null)
                startDate = endDate.Value.AddDays(-1);

            var data = await _s.getWeekOrders(usrid, startDate.Value, endDate.Value);

            return new BaseResult() { Data = data, DataCount = data.Count, IsSuccess = true };
        }

        [HttpGet()]
        public async Task<BaseResult>  getAllFinance(){
            return await _s.getAllFinance();
        }

        [HttpPost()]
        public async Task<dynamic> InsertCheck(endLog model)
        {
			DateTime endtime = DateTime.Now;
			endtime = new DateTime(endtime.Year, endtime.Month, endtime.Day, endtime.Hour, endtime.Minute, 0);
			int count = 0;
			if (endtime.Minute % 5 == 0)
				count = 5; //刚好是5分的时候
			else
				count = 5 - endtime.Minute % 5;

			endtime = endtime.AddMinutes(count);

			endtime = endtime.AddHours(-8);
            model.date = endtime;

            var e = await _s.insertCheck(model);
            return new BaseResult() { IsSuccess = true };
        }


    }
}
